<template>
  <el-main>
    
    <el-row>
        <el-col :span="18"><el-input v-model="searchText" placeholder="请输入要查找的姓名" auto-complete="false"/></el-col>
         <el-col :span="3"> &nbsp;   </el-col>
       <el-col :span="3"><el-button type="primary">搜索</el-button>
       <el-button  type="success" @click="addBtnClick()">新增</el-button> </el-col>
      
    </el-row>
    <el-row>
        <el-col :span="24">
        <el-table :data="userList" stripe style="width:100%"
        max-height="250">
     
        <el-table-column
          prop="id"
          label="工号">
        </el-table-column>
        
        <el-table-column
          prop="name"
          label="姓名">
        </el-table-column>

        <el-table-column
          prop="age"
          label="年龄">
        </el-table-column>

        <el-table-column 
          label="操作" fixed="right">
       <template slot-scope="scope">
        <el-button type="primary" @click="modifyBtnClick(scope.$index)" plain>修改</el-button>
        <el-button type="danger" @click="deleteBtnClick(scope.$index)" plain>删除</el-button>
       </template>

        </el-table-column>

        </el-table>
    </el-col>
    </el-row>
    <el-row>
    <el-col :span="24">
   <el-card class="box-card">
   </el-card>
    </el-col>    
    </el-row>
    <div>
    </div>

    <!-- dialog -->
    <el-dialog :title="userForm.title" :visible.sync="userForm.visible">
    <el-form >
    <el-form-item label="工号">
     <el-input type="text" v-model="userForm.data.id" readonly/>
    </el-form-item>
        <el-form-item label="姓名">
     <el-input type="text" v-model="userForm.data.name" />
    </el-form-item>
        <el-form-item label="年龄">
     <el-input-number v-model="userForm.data.age" />
    </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="resetForm">返回</el-button>
      <el-button @click="submitForm" type="primary">确认</el-button>
    </div>
    </el-dialog>
  </el-main>
</template>

<script>
export default {
    name:'UserContent',
    data() {
        return {
            searchText:'',
            userForm:{
              title: "",
              visible:false,
              method:"",
              data:{
               id:"",
               name:"",
               age:0
              }
      
            },
            userList:[
                {id:1,name:'张三',age:15},
                {id:2,name:'李四',age:17},
                {id:3,name:'王五',age:25},
                {id:4,name:'赵六',age:34}
            ]
        }
    },
    methods: {
        addBtnClick(){
         this.userForm.data.id=null
         this.userForm.data.name=""
         this.userForm.data.age=0
         this.userForm.title="新增用户"
         this.userForm.visible=true 
         this.userForm.method="put"
        },
        modifyBtnClick(id){
          console.log(id);
          let user = this.userList[id]
         this.userForm.data.id=user.id
         this.userForm.data.name=user.name
         this.userForm.data.age=user.age
         this.userForm.title="修改用户信息"
         this.userForm.visible=true 
          this.userForm.method="post"
        },
        resetForm(){
        this.userForm.data.id=null
         this.userForm.data.name=""
         this.userForm.data.age=0
          this.userForm.title=""
           this.userForm.visible=false
        },
        getUserList(){
            this.$axios.get('/user').then((response)=>{
             
              this.userList=response.data.data
               console.log(this.userList);
           })
        },
        submitForm(){
          console.log(this.userForm.data);
           this.$axios({
            method:this.userForm.method,
             url:'/user',
             data:{
              id: this.userForm.data.id,
              name: this.userForm.data.name,
              age: this.userForm.data.age
             }
           }).then((response)=>{
            console.log(response.data)
             this.resetForm()
           })
        }
    },
    mounted(){
        this.getUserList()
    }

}

</script>

<style>

</style>